home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_2 / gadmget / autoftp.rexx next >
OS/2 REXX Batch file  |  1995-08-05  |  10KB  |  311 lines

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /* AutoFTP.rexx : an ARexx script to automatically a list of files from an   */
  4. /*                Aminet archive site.  When run, the new RECENT listing of  */
  5. /*           the site is downloaded, and GadMGet is run on that         */
  6. /*                listing.  After GadMGet is terminated, GadMGet's output is */
  7. /*                sent to ncFTP, which automatically connects to the site    */
  8. /*                and downloads the programs specified by the user.          */
  9. /*                                                                           */
  10. /*   Don't use AutoFTP unless you have AmiTCP, GadMGet, and ncFTP already    */
  11. /*   working correctly.  AutoFTP relies on these programs to work.           */
  12. /*                                                                           */
  13. /*                                                                           */
  14. /*   Command line Switches accepted :                                        */
  15. /*          YES -- Automatically download a new RECENT file                  */
  16. /*          NO  -- Don't download a RECENT file                              */
  17. /*        DEBUG -- Write all AmigaDOS commands executed to stdout            */
  18. /*                                                                           */                         
  19. /*   If neither of the above is specified, a requester will be displayed     */
  20. /*   asking the user whether or not to get a new RECENT file or not.         */
  21. /*                                                                           */                         
  22. /*****************************************************************************/
  23.  
  24.  
  25. /**************************************************************************/
  26. /* ---------- ADJUST THESE PARAMETERS TO FIT YOUR PREFERENCES ----------- */
  27. /**************************************************************************/
  28.  
  29. /* The Aminet FTP Site you prefer to use */
  30. AminetSite = "ftp.netnet.net"
  31.  
  32. /* The root directory of Aminet within that site */
  33. AminetRootDir = "/pub/aminet"
  34.  
  35. /* The directory you want your copy of RECENT to be in */
  36. LocalRecentDir = "dh0:"            
  37.  
  38. /* Path & Filename of GadMGet executable */
  39. GadMGet = "GadMget" 
  40.  
  41. /* Path & Filename of ncFTP executable */
  42. ncFTP = "amitcp:bin/ncFTP"
  43.  
  44. /* Any miscellaneous startup arguments you wish to be included 
  45.    on GadMGet's command line call.  See GadMGet documentation on
  46.    "Startup Options" for a list of possible options. */
  47. GadMGetArgs = ""  
  48.  
  49. /* The directory path you want AutoFTP to download files to. */
  50. DownloadDir = "dh0:"
  51.  
  52. /* Do you want AutoFTP to keep saved selected lines from previous 
  53.    GadMget sessions? */
  54. PreserveSelected = YES
  55.  
  56. /* Do you want AutoFTP to remove lines from the new RECENT file that you
  57.    already have selected in the old RECENT file?  (This option is only
  58.    significant if PreserveSelected is YES) */
  59. DoPruning = YES
  60.  
  61.  
  62. /**************************************************************************/
  63. /* ------- END USER PARAMETERS --- DO NOT MODIFY BELOW THIS LINE -------- */
  64. /**************************************************************************/
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. /**************************************************************************/
  85. /* ---------------------- BEGIN AUTOFTP SCRIPT CODE --------------------- */
  86. /**************************************************************************/
  87.  
  88. options results
  89. parse arg CommandLineArg
  90.  
  91. DownloadRECENT = MAYBE
  92. Debug = FALSE
  93.  
  94. CommandLineArg = upper(CommandLineArg)
  95.  
  96. if (CommandLineArg == "?") then do 
  97.     say "AutoFTP:  AutoFTP YES/S NO/S DEBUG/S" 
  98.     exit
  99.     end
  100. if (CommandLineArg == "YES") then DownloadRECENT = YES
  101. if (CommandLineArg == "NO")  then DownloadRECENT = NO
  102. if (CommandLineArg == "DEBUG") then do
  103.     say "AutoFTP:  Debug mode on."
  104.     Debug = TRUE
  105.     end        
  106.  
  107. if (DownloadRECENT == MAYBE) then do
  108.     /* Load rexxreqtools.library if possible */
  109.     if (show('L','rexxreqtools.library')) then RexxReqToolsLoaded = 1
  110.         else RexxReqToolsLoaded = addlib('rexxreqtools.library',0,-30)
  111.     
  112.     if (RexxReqToolsLoaded) then UserResponse = rtezrequest('Shall I download the new RECENT file now?',"_Yes|_No")
  113.        else do
  114.            say "[Warning: rexxreqtools.library not available!]"
  115.            say "Shall I download a new RECENT file now?  (y/n)"
  116.            pull UserResponse
  117.            UserString = upper(UserResponse)
  118.            UserResponse = 1
  119.            if (left(UserString,1) == "N") then UserResponse = 0
  120.        end
  121.         
  122.     if (UserResponse == 1) then DownloadRECENT = YES 
  123.                    else DownloadRECENT = NO
  124.     end
  125.  
  126.  
  127. /* Fix up the syntax of some parameters */
  128. if (right(AminetSite,1) ~= ":") then    AminetSite    = AminetSite    || ":"
  129. if (right(AminetRootDir,1) ~= "/") then AminetRootDir = AminetRootDir || "/"
  130.  
  131. /* Get a nice temporary filename or two */
  132. ProcessID = time('S')
  133. TempFile = "t:AutoFTP.temp." || ProcessID
  134. BackupFile = "t:AutoFTP.script.bak"
  135. TempRecent = "t:AutoFTP.RECENT." || ProcessID
  136.  
  137. GadMGetArgs = GadMGetArgs || " NOSIMPLEPATHS AMINETPATH " || AminetRootDir || " OUTPUT " || TempFile
  138. LocalRecentFile = LocalRecentDir || "RECENT"
  139.  
  140. if (DownloadRECENT == YES) then do
  141.     /* Construct request for RECENT */
  142.     RECENTfile = AminetSite || AminetRootDir || "RECENT"
  143.  
  144.     say "Retrieving the current RECENT file from " || AminetSite
  145.     GetRECENTCommand = ncFTP || " -r " || RECENTfile
  146.  
  147.     /* Get RECENT file */
  148.     call Pragma('D',"t:")
  149.     success = DoCommand(GetRECENTCommand)
  150.     RenameCommand = "rename RECENT " || TempRecent || " QUIET"
  151.     success = DoCommand(RenameCommand)
  152.     call Pragma('D',LocalRECENTdir)
  153.  
  154.     /* Now either process the downloaded RECENT file to save selected lines,
  155.        or if that's turned off, just copy it over. */
  156.     if (PreserveSelected = YES) then do
  157.         success = SmartCopy(LocalRecentFile,TempRecent,LocalRecentFile)
  158.         if (success == 0) then do
  159.             say "WARNING:  File Merge failed.  Falling back to simple copy"
  160.             CopyCommand = "copy " || TempRecent || " " || LocalRecentFile || " QUIET"
  161.             success = doCommand(CopyCommand)
  162.             end    
  163.         end
  164.     else do
  165.         CopyCommand = "copy " || TempRecent || " " || LocalRecentFile || " QUIET"
  166.         success = doCommand(CopyCommand)
  167.         end
  168.  
  169.     DeleteTempRecentCommand = "delete " || TempRecent || " QUIET"
  170.     success = DoCommand(DeleteTempRecentCommand)    
  171.     end
  172.     
  173. /* Now we run GadMGet */
  174. GadMGetCommand = GadMGet || " " || LocalRecentFile || " " || GadMGetArgs
  175. success = DoCommand(GadMGetCommand)
  176.  
  177. /* Make sure that the output file is there */
  178. if (~Exists(TempFile)) then do
  179.     say "You didn't select any files.  Exiting..."
  180.     exit
  181.     end
  182.     
  183.     
  184. /* Make a backup copy of GadMGet's output, that way if the ftp session
  185.    breaks, at least the user still has the last output to use manually
  186.    is s/he wants. */
  187. BackupCommand = 'copy ' || TempFile || " " || BackupFile || " QUIET"
  188. success = DoCommand(BackupCommand)
  189.  
  190. /* Let the user see the list... */
  191. say "------FTP script is as follows-----"
  192. address COMMAND "type " TempFile
  193. say "-----------End FTP script----------"
  194.  
  195. /* Change to the directory the user wishes to download to */
  196. call Pragma('D',DownloadDir)
  197. DownloadCommand = ncFTP || " -r " || AminetSite || AminetRootDir || " <" || TempFile
  198. success = DoCommand(DownloadCommand)
  199.  
  200. /* Now delete the Temporary file */
  201. DeleteCommand = "delete " || TempFile || " QUIET"
  202. success = DoCommand(DeleteCommand)
  203.  
  204.  
  205. say "AutoFTP script terminating."
  206. exit
  207.  
  208.  
  209.  
  210.  
  211. /**************************************************************************/
  212. /* ------------------------ BEGIN PROCEDURES ---------------------------- */
  213. /**************************************************************************/
  214.  
  215.  
  216.  
  217.  
  218.  
  219. /* Executes an AmigaDos shell command for us, giving output if called for. */
  220. DoCommand: procedure expose Debug
  221.     parse arg thiscommand
  222.     if (Debug == TRUE) then say "EXECUTING: [" || thiscommand || "]"    
  223.     address COMMAND thiscommand
  224.     return 1
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234. /* Looks at the current RECENT and the new one, and merges them so that: 
  235.     1) All currently selected lines in the current RECENT are in the
  236.        output file's selected section
  237.     2) All currently selected lines in the current RECENT are NOT in
  238.        the output file's selectable section.   */
  239. SmartCopy: procedure expose Debug DoPruning
  240.     parse arg OldRecent, NewRecent, OutFile .
  241.  
  242.     if (Debug == TRUE) then say "Merging " || OldRecent || " and " || NewRecent || " into " || Outfile
  243.  
  244.     /* This needs to be accurate! */
  245.     BOOKMARK = "| --- GadMGet: Begin Selected Files --- "
  246.     
  247.     /* Load OldRecent into OldRecentSel  */
  248.     OSelIndex = 0
  249.     OKeepInput = 0
  250.     if ~open(OldInput,  OldRecent,  'R') then return 0
  251.           do until eof(OldInput)
  252.             ThisLine = readln(OldInput)
  253.             if (ThisLine == BOOKMARK) then do
  254.                 OKeepInput = 1
  255.                 end
  256.             else do
  257.                 if (OKeepInput == 1) then do
  258.                     OldRecentSel.OSelIndex = ThisLine
  259.                     OSelIndex = OSelIndex + 1
  260.                 end
  261.             end    
  262.         end 
  263.     call close(OldInput)
  264.  
  265.     if (OSelIndex == 0) then do
  266.         /* No selected lines to save.  Might as well take the short cut */
  267.         CopyCommand = "copy " || NewRecent || " " || OutFile
  268.         success = DoCommand(CopyCommand)
  269.         return 1
  270.         end
  271.     
  272.     /* Load NewRecent into NewRecent.Reg.  Presumably since we just
  273.        downloaded this from Aminet, there won't be a NewRecent.Sel ! */
  274.     NewRecentRegIndex = 0
  275.     if ~open(NewInput, NewRecent, 'R') then return 0
  276.     if ~open(NewOutput,OutFile,   'W') then do
  277.         call close(NewInput)
  278.         return 0
  279.         end
  280.           do until eof(NewInput)
  281.             ThisLine = readln(NewInput)
  282.         CheckIndex = 0
  283.         OKToPrint = 1
  284.         if (DoPruning == YES) then do
  285.             do while ((CheckIndex < OSelIndex)&(OKToPrint = 1))
  286.                 if (OldRecentSel.CheckIndex == ThisLine) then OKToPrint = 0
  287.                 CheckIndex = CheckIndex + 1
  288.                 end
  289.             end
  290.         if (OKToPrint == 1) then call writeln(NewOutput,ThisLine)
  291.         end 
  292.     call close(NewInput)
  293.     
  294.     /* Now write the bookmark, and our selected files */
  295.     call writeln(NewOutput,BOOKMARK)
  296.     WriteIndex = 0
  297.     do while (WriteIndex < OSelIndex)
  298.         call writeln(NewOutput,OldRecentSel.WriteIndex)
  299.         WriteIndex = WriteIndex + 1
  300.         end
  301.     call close(NewOutput)
  302.     return 1    
  303.     
  304.  
  305.  
  306.  
  307.  
  308. /**************************************************************************/
  309. /* ------------------------ END AUTOFTP SCRIPT CODE --------------------- */
  310. /**************************************************************************/
  311.